package autoteam.ui.dialogs;

import java.awt.*;
import javax.swing.*;

import db.*;
import autoteam.tables.AutoTeamTableModel;

public class SearchPlayerDialog extends AutoTeamDialog
{
	private static final long serialVersionUID = 1L;
	
	private DataAccessObject dao;
	
	private JLabel player;
	private final JLabel playerIdLabel = new JLabel("Player ID:");
	private final JLabel heightLabel = new JLabel("Height:");
	private final JLabel weightLabel = new JLabel("Weight:");
	private final JLabel expLabel = new JLabel("Experience:");
	private final JLabel availLabel = new JLabel("Availability:");
	private JLabel[] skillLabels;
	private final JLabel phoneLabel = new JLabel("Phone:");
	private final JLabel emailLabel = new JLabel("Email:");
	
	private JLabel playerId;
	private JLabel height;
	private JLabel weight;
	private JLabel exp;
	private JLabel avail;
	private JLabel[] skills;
	private JLabel phone;
	private JLabel email;
	
	public SearchPlayerDialog(JFrame dialogOwner, AutoTeamTableModel model, int row)
	{
		super(dialogOwner);
		
		String temp = (String) model.getValueAt(row, 0);
		int playId = Integer.parseInt(temp.substring(temp.indexOf('(') + 1, temp.indexOf(')')));
		player = new JLabel(temp.substring(0, temp.indexOf('(') - 1));
		playerId = new JLabel(((Integer) playId).toString());
		
		dao = new DataAccessObject();
		ColumnData colId = new ColumnData("playerId", playId);
		DBRecord record = dao.getRecord("players", colId);
		
		String tempHeight = ((Integer) record.getFieldValue("height")).toString();
		tempHeight = tempHeight.charAt(0) + "'" + tempHeight.substring(1, tempHeight.length()) + "\"";
		height = new JLabel(tempHeight);
		weight = new JLabel(((Integer) record.getFieldValue("weight")).toString());
		exp = new JLabel(((Integer) record.getFieldValue("experience")).toString());
		phone = new JLabel((String) record.getFieldValue("phone"));
		email = new JLabel((String) record.getFieldValue("email"));
		
		String nights = "";
		if ((Integer) record.getFieldValue("monday") == 1)
			nights += "M ";
		if ((Integer) record.getFieldValue("tuesday") == 1)
			nights += "T ";
		if ((Integer) record.getFieldValue("wednesday") == 1)
			nights += "W ";
		if ((Integer) record.getFieldValue("thursday") == 1)
			nights += "R ";
		if ((Integer) record.getFieldValue("friday") == 1)
			nights += "F";
		
		if (nights.equals(""))
			nights = "Not Available";
		avail = new JLabel(nights);
		
		String s = (String) record.getFieldValue("skill");
		
		if (!s.equals(""))
		{
			String[] token = s.split(", ");
		
			skillLabels = new JLabel[token.length];
			skills = new JLabel[token.length];
		
			char[] nums = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
			for (int i = 0; i < token.length; i++)
			{
				String str = token[i];
				int index = 0;
				int lowIndex = str.length() - 1;
				for (int j = 0; j < 10; j++)
				{
					index = str.indexOf(nums[j]);
					if (index != -1)
					{
						if (lowIndex > index)
							lowIndex = index;
					}
				}
				skillLabels[i] = new JLabel(str.substring(0, lowIndex) + ":");
				skills[i] = new JLabel(str.substring(lowIndex, str.length()));
			}
		}
		else
		{
			skillLabels = new JLabel[0];
			skills = new JLabel[0];
		}
		
		// set dialog preferences
		int r = 7 + skills.length;
		int dialogWidth = 400;
		int dialogHeight = 170 + (30 * r);
		this.setDialogSize(new Dimension(dialogWidth, dialogHeight));
		Toolkit sizer = Toolkit.getDefaultToolkit(); 
		Dimension screenSize = sizer.getScreenSize();
		int screenCenterX = (screenSize.width - dialogWidth) / 2;
		int screenCenterY = (screenSize.height - dialogHeight) / 2;
		this.setLocation(screenCenterX, screenCenterY);
		
		// set layout preferences
		setLayout(null);
		int x = 50;		// x coordinate
		int y = 50;		// y coordinate
		int xGap = 100;	// gap between columns
		int yGap = 30;	// gap between rows
		
		int x1 = 100;	// column one width
		int x2 = 200;	// column two width
		int y1 = 20;	// row height
		
		int i = 0;
		player.setBounds(x, y, x1 + x2, y1);
		player.setHorizontalAlignment(JLabel.CENTER);
		i++;
		i++;
		playerIdLabel.setBounds(x, y + (yGap * i), x1, y1);
		playerId.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		heightLabel.setBounds(x, y + (yGap * i), x1, y1);
		height.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		weightLabel.setBounds(x, y + (yGap * i), x1, y1);
		weight.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		expLabel.setBounds(x, y + (yGap * i), x1, y1);
		exp.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		for (int j = 0; j < skills.length; j++)
		{
			skillLabels[j].setBounds(x, y + (yGap * i), x1, y1);
			skills[j].setBounds(x + xGap, y + (yGap * i++), x2, y1);
		}
		availLabel.setBounds(x, y + (yGap * i), x1, y1);
		avail.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		phoneLabel.setBounds(x, y + (yGap * i), x1, y1);
		phone.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		emailLabel.setBounds(x, y + (yGap * i), x1, y1);
		email.setBounds(x + xGap, y + (yGap * i), x2, y1);
		
		// add components
		add(player);
		add(playerIdLabel);
		add(playerId);
		add(heightLabel);
		add(height);
		add(weightLabel);
		add(weight);
		add(expLabel);
		add(exp);
		for (int j = 0; j < skills.length; j++)
		{
			add(skillLabels[j]);
			add(skills[j]);
		}
		add(availLabel);
		add(avail);
		add(phoneLabel);
		add(phone);
		add(emailLabel);
		add(email);
	}
	
	@Override
	protected void setButtonActions(AutoTeamDialog thisDialog)
	{
		// TODO Auto-generated method stub
		
	}
}
